package com.weixin1.servlet;

import com.weixin1.bean.News;
import com.weixin1.dto.R;
import com.weixin1.service.NewsService;
import com.weixin1.utils.JSONUtil;
import com.weixin1.utils.MyBeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

@WebServlet("/newsservlet/*")
@MultipartConfig
public class NewsServlet extends HttpServlet {
    NewsService ns=new NewsService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String pathInfo = req.getPathInfo().substring(1);
        if ("list".equals(pathInfo)){
            list(req,resp);
        }else if ("upload".equals(pathInfo)){
            upload(req,resp);
        }else if("save".equals(pathInfo)){
            save(req,resp);
        }else if ("update".equals(pathInfo)){
            update(req,resp);
        }else if ("removeById".equals(pathInfo)){
            removeById(req,resp);
        }else if("content".equals(pathInfo)){
            content(req,resp);
        }else if("getById".equals(pathInfo)){
            getById(req,resp);
        }
    }

    //根据id查询咨询对象
    private void getById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long nId = Long.valueOf(req.getParameter("nId"));
        try {
            News news = ns.getById(nId);
            List<News> list = new ArrayList<>();
            list.add(news);
            Map<String,Object> map=new HashMap<>();
            map.put("list",list);
            resp.getWriter().print(JSONUtil.toString(map));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void content(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            List<News> list = ns.list();
            Map<String,Object> map=new HashMap<>();
            map.put("list",list);
            resp.getWriter().print(JSONUtil.toString(map));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //根据请求获取到的id删除对象
    private void removeById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long nId = Long.valueOf(req.getParameter("nId"));
        try {
            boolean b = ns.removeById(nId);
            if (b){
                resp.getWriter().print(JSONUtil.toString(R.ok()));
            }else {
                resp.getWriter().print(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        News news=MyBeanUtils.reqToBean(req,News.class);
        try {
            boolean b = ns.update(news);
            if (b){
                resp.getWriter().print(JSONUtil.toString(R.ok()));
            }else {
                resp.getWriter().print(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        News news= MyBeanUtils.reqToBean(req,News.class);
        try {
            boolean b = ns.save(news);
            if (b){
                resp.getWriter().print(JSONUtil.toString(R.ok()));
            }else {
                resp.getWriter().print(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void upload(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Part part = req.getPart("file");
        Map<String,Object> map = new HashMap<>();
        String fileName = part.getSubmittedFileName();
        String ext = fileName.substring(fileName.lastIndexOf("."));
        String path="F:/javawebproject/src/main/webapp/admin/upload/" + UUID.randomUUID() + ext;
        part.write(path);

        map.put("path",path);
        map.put("code",0);

        resp.getWriter().print(JSONUtil.toString(map));
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        try {
            List<News> news = ns.selectByPage(page, limit);
            int total = ns.gettotal();
            Map<String, Object> map = new HashMap<>();

            map.put("code",0);
            map.put("data",news);
            map.put("count",total);
            resp.getWriter().print(JSONUtil.toString(map));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
